
var $file = $('#fileUpload');
var $upload = $('#btnUpload');

$upload.on('click',function(){
	$file.click();
	return false;
});

$file.on('change',function(){
	readURL2(this);
	return false;
});

function readURL2(input)
{
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            path = e.target.result;
            $('#out').html('<img src = "'+ path + '"/>');	
        }
        reader.readAsDataURL(input.files[0]);
    }
}


$('#info').on("input",function(){	
	
	var text = $(this).val();
	var reg = /(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/;
	if(reg.test(text)){
		$('#postLoading').show();
		$.ajax({
			url: URL + 'feeds/convert',
			data: {text:text},
			type: 'get',
			dataType: 'json',
			success: function(data){
				if(data.status == true)
				{	
					$('#out').html(data.html);	
					$('#postLoading').hide();
				}
			}
		});
	}
});  

$('#btnPost').click(function(){
	var comments = $('#info').val().replace(/(<([^>]+)>)/ig,"");
	if(comments.trim() == '') return false;
	$('#postLoading').show();
	
	var html = $('#out').html();
	$.ajax({
		url: URL + 'feeds/insert',
		type: 'post',
		dataType: 'json',
		data: {content:Linkify(comments),html:html,receiver:SS_USER_ID},
		success: function(result){
			if(result.status == false){
				alert(result.messages);
			}else if(result.status == true){
				
			}
				$('#info').val('');
				$('#out').html('');
				$('#postLoading').hide();
		}
	});
	
	return false;
});

$(document).on('keypress','.commentbox',function(e) {
	
	  if(e.which == 13)
	  {	
	  	 var comment = $(this).val();
	  	 var feed_id = $(this).attr('id');
	  	 var receiver_id = $(this).attr('receiver-id'); //for notification

	  	 var sender_id = SS_USER_ID;
	  	 
	  	 if(!$.trim(comment) == '')
	  	 {
	  	 	$.ajax({
	  	 		url: URL + 'comments/insert',
	  	 		type: 'post',
	  	 		data:{comments:comment,feed_id:feed_id,receiver_id:receiver_id,sender_id:sender_id},
	  	 		dataType: 'json',
	  	 		success: function(result){
	  	 			if(result.status == true)
	  	 			{
	  	 				
	  	 			}
	  	 			else
	  	 			{
	  	 				//alert(result.messages);
	  	 			}
	  	 		}	
	  	 	})
	  	 }
	  	 $(this).val('');
	  	 $('.commentbox').blur();
	  }

});
 
function add_comment(data)
{
	var template = $('#commentTpl').html();
	var html = Mustache.to_html(template,data);
	$('#feeds_comment_'+data.feeds_id).append(html).hide().fadeIn('slow');
}

function status_update(comments,html,data)
{
	 var template = $('#postTpl').html();

	 if(data.feed_type == "wall_posts")
	 {
		template = $('#postTpl2').html();
	 }
 	 var data = {
 	 	comments:comments,
 	 	html:html,
 	 	data:data	
 	 }
    var html = Mustache.to_html(template, data);
	$(html).prependTo('#postContainer').hide().fadeIn('slow');
}

function event_update(data)
{
	var template = $('#eventsTpl').html();
    var html = Mustache.to_html(template, data);
	$(html).prependTo('#postContainer').hide().fadeIn('slow');	
}

$(document).ready(function(){
	
	jQuery("abbr.timeago").timeago();
	$('#postLoading').hide();
	var pusher = new Pusher(PUSHER_APP_KEY);
	var channel = pusher.subscribe('my-channel');
	
	channel.bind('comment-added', function(data) {
		console.log(data);
	 	add_comment(data);
	 	jQuery("abbr.timeago").timeago();
	});

	channel.bind('status-update', function(data) {
			var sender_id = data.user_id;
			$.ajax({
				url: URL + 'connections/check',
				data:{sender_id:sender_id},
				type:'post',
				success: function(result){
					if(result == "1" || data.user_id == SS_USER_ID )
					{	
						status_update(data.feed_title,data.feed_container,data);
						jQuery("abbr.timeago").timeago();						
					}
				}	
			});
	});

	channel.bind('new_event', function(data) {
			var receivers = data.receivers.split(",");
			var a = receivers.indexOf(SS_USER_ID)
			if(a >= 0){
				event_update(data);
			}
	});
	channel.bind('likes-update', function(data) {
		var current = SS_USER_ID;
		$('#feeds_total_likes_'+data.feed_id).text(data.total_likes + ' People like this');
		if(current == data.user_id){
			if(data.type == 'like')
			{
				$('#feed_like_id_'+data.feed_id).text('Unlike');
			}
			else if(data.type ="unlike")
			{
				$('#feed_like_id_'+data.feed_id).text('Like');
			}
		}
	});


}); // end of document ready

function loadMore(page_no)
{

	return $.ajax({
		type: 'post',
		url: URL + 'feeds/list',
		data: {page_no:page_no	},
		dataType: 'json'	
	});
}

function getHtml(comments,html,data)
{
	 var template = $('#postTpl').html();

	 if(data.feed_type == "wall_posts")
	 {
		template = $('#postTpl2').html();
	 }
 	 var data = {
 	 	comments:comments,
 	 	html:html,
 	 	data:data	
 	 }
    var html = Mustache.to_html(template, data);
    return html;
}

jQuery(document).ready(function() {
	jQuery("abbr.timeago").timeago();
		var loading = false;

		var $loadmore = $('#loadmore');

			$loadmore.hide();
			// infinite loading
			$(window).scroll(function(){
				if($(window).scrollTop() == $(document).height() - $(window).height())
				{
					// load more function
					$loadmore.show();

					//load data
					var page_no = parseInt($('#page_no').val());

					if(page_no == 1){
						page_no += 1; // 
					}
					//alert(page_no);
					if(loading) return;
					loading = true;
					
					loadMore(page_no).done(function(result){
					
					if(result.length > 0){
						
						var content = '';
						$(result).each(function(i,val){
							content += getHtml(val.feed_title,val.feed_container,val);
						});
						if(content != ''){
							page_no += 1;
							$('#page_no').val(page_no.toString());
							$(content).appendTo('#postContainer').hide().fadeIn('slow');
							$loadmore.hide();
							loading = false;
							jQuery("abbr.timeago").timeago();
						}
					}else{
						loading = false;
						$loadmore.hide();
					}
						return false;
					});
				
					return false;

				}else{
					$loadmore.hide();
				}
			});
});

$(document).on("click",".btnLike",function(){
	var feed_id = $(this).attr('feed-like-id');
	var text = $(this).text();
	if(text == "Like")
	{
		$(this).text('Unlike');
		$.ajax({
			url: URL+'likes/insert',
			data: {feed_id:feed_id},
			type: 'post',
			dataType: 'json',
			success: function(result){
				if(result.status == true)
				{
					
				}
				else
				{
					
				}
			}
		})
	}
	else if(text == 'Unlike')
	{
		$(this).text('Like');
		$.ajax({
			url: URL+'likes/delete',
			data: {feed_id:feed_id},
			type: 'post',
			dataType: 'json',
			success: function(result){
				if(result.status == true)
				{
			
				}
				else
				{
					
				}
			}
		})	
	}
});
